<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-25 20:19:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 20:22:43
 * @Descripttion: 
 */


namespace app\common\model;

use think\Model;

use cznet\IpLocation;
class LogApp extends Base
{
    protected $insert = ['addtime','ip','province','city','area'];
    protected $state_arr = [
        0 => "正常",
        1 => "禁用",
    ];
    protected $type_arr = [
        'user' => "消费者",
        'manage' => "销售经理",
        'bus' => "经销商",
    ];
    //---------------自动完成----------------------
    protected function setAddtimeAttr()
    {
        return time();
    }
    protected function setIpAttr()
    {
       return $_SERVER['REMOTE_ADDR'];
    }
    protected function setProvinceAttr()
    {
        return $this->getMethod_3('124.234.181.155','province');
    }
    protected function setCityAttr()
    {
        return $this->getMethod_3('124.234.181.155','city');
    }
    protected function setAreaAttr()
    {
        return $this->getMethod_3('124.234.181.155','district');
    }
    public function getAddtimeTextAttr($value, $data){
        return date('Y-m-d H:i:s', $data['addtime']);
    }
    
    public function getStateTextAttr($value,$data){
        return $this->state_arr[$data['state']];
    }
    public function setTypeAttr($value,$data){
        return empty($value) ? 'user' : $value;
    }

    public function getTypeTextAttr($value,$data){
        return $this->type_arr[$data['type']];
    }
    function getMethod_3($ip,$value) {
        $response = $this->cUrlGetIP('https://ipchaxun.com/'.$ip.'/');
        $str1 = substr($response, strripos($response, "归属地") + 15);
        $str2 = substr($str1, 0, strrpos($str1, "运营商"));
        // 提取省份
        $provinces = array(
            "北京",
            "天津",
            "河北",
            "山西",
            "内蒙古",
            "辽宁",
            "吉林",
            "黑龙江",
            "上海",
            "江苏",
            "浙江",
            "安徽",
            "福建",
            "江西",
            "山东",
            "河南",
            "湖北",
            "湖南",
            "广东",
            "广西",
            "海南",
            "重庆",
            "四川",
            "贵州",
            "云南",
            "西藏",
            "陕西",
            "甘肃",
            "青海",
            "宁夏",
            "新疆",
            "香港",
            "澳门",
            "台湾"
        );
        $province = '';
        foreach ($provinces as $province_) {
            if (strpos($str2, $province_) !== false) {
                $province = $province_;
                break;
            }
        }
        
        // 提取国家
        $str3 = substr($str2, 0, strrpos($str2, $province));
        $country = preg_replace('/[^\x{4e00}-\x{9fa5}]+/u', '', $str3);
        
        // 提取城市
        $str4 = substr($str2, strripos($str2, "nofollow") + 10);
        $city = substr($str4, 0, strrpos($str4, "</a>"));
        
        // 提取县区
        $str6 = substr($str2, strripos($str2, "</a>") + 4);
        $district = substr($str6, 0, strrpos($str6, "</span>"));
        $data = [
            'province' => $province,
            'city' => $city,
            'district' => $district,
        ];
        return $data[$value];
    
        
        
    }

    function cUrlGetIP($url) {
        
        // cUrl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $header[] = 'user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        return curl_exec($ch);
        curl_close($ch);
    }


  
}
